export class Router {
    constructor(routes) {
        this.routes = routes;
        this.currentRoute = null;
        this.rootElement = document.getElementById('app');
        
        window.addEventListener('hashchange', () => this.handleRoute());
        this.handleRoute();
    }

    handleRoute() {
        const hash = window.location.hash.slice(1) || '/';
        const route = this.routes[hash] || this.routes['/'];
        
        if (this.currentRoute) {
            // 移除之前的页面内容
            this.rootElement.innerHTML = '';
        }
        
        this.currentRoute = route;
        this.rootElement.appendChild(route.render());
    }

    unmount() {
        this.rootElement.innerHTML = '';
        this.currentRoute = null;
    }
}